package org.tedu.egmvcmvc01.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.tedu.egmvcmvc01.mapper.AddressMapper;
import org.tedu.egmvcmvc01.pojo.dto.AddressDTO;
import org.tedu.egmvcmvc01.pojo.entity.Address;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/address/insert")
    public String insert(AddressDTO addressDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        int num = addressMapper.insertAddress(address);
        return num > 0 ? "插入成功" : "插入失败";
    }


    @RequestMapping("/address/delete")
    public String delete(int id) {
        int num = addressMapper.deleteAddress(id);
        return num > 0 ? "删除成功" : "删除失败";
    }


    @RequestMapping("/address/update")
    public String update(Address address) {
        int num = addressMapper.updateAddress(address);
        return num > 0 ? "修改成功" : "修改失败";
    }


    @RequestMapping(value = "/address/select", method = RequestMethod.GET)
    public List<Address> select() {
        List<Address> userList = addressMapper.selectAddresses();
        return userList;
    }
}
